package org.grc.app.z_project.z_framework.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkDynamicProxy implements InvocationHandler {

	private Object target = null;
	
	public JdkDynamicProxy (Object target) {
		this.target = target;
	}

	@SuppressWarnings("unchecked")
	public <T> T getProxy () {
		return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), 
				target.getClass().getInterfaces(), this);
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		before();
		Object result = method.invoke(target, args);
		after();
		return result;
	}
	
	private void before () {
		System.out.println("JDK Proxy Before");
	}
	
	private void after () {
		System.out.println("JDK Proxy After");
	}
}
